#include <BlockBitmap.h>
#include <iostream>

using namespace std;

//获得一个空块的虚拟地址
int BlockBitmap::get()
{
    int pos = -1;
    for(int i = 0; i < 128; i++)
    {
        if(bytes[i] != -1)
        {
            for(int j = 0; j < 8; j++)
            {
                char ch = bytes[i] << j;
                if(ch >= 0)
                {
                    pos = 8 * i + (7 - j);
                    return pos;
                }
            }
        }
    }
    
    return pos;
}

//传入一个虚拟地址，设置其位图的值
void BlockBitmap::set(int pos, bool bit)   // pos从 [0 , 1023]
{
    int index  = pos / 8;
    int offset = pos % 8;
    if(bit)
    {
        char ch = 1 << offset;
        bytes[index] |= ch;
    }else
    {
        char ch = 1 << offset;
        ch = ~ch;
        bytes[index] &= ch;
    }   
}